﻿using AutoMapper;
using Microsoft.EntityFrameworkCore.Internal;
using ProjectManageSystemApi.DbEntity;
using ProjectManageSystemApi.Enums;
using ProjectManageSystemApi.Models;
using ProjectManageSystemApi.Models.BuildingSiteBillModel;
using ProjectManageSystemApi.Models.PreFinalAccountFileModel;
using ProjectManageSystemApi.Models.ProjectItemPriceRecordModel;
using ProjectManageSystemApi.Models.ProjectUserInfo;
using ProjectManageSystemApi.Models.PurchaseOrderModel;
using ProjectManageSystemApi.Models.UserCheckDetailModel;
using ProjectManageSystemApi.Models.WorkerType;
using System;
using System.Linq;

namespace ProjectManageSystemApi
{
    public class AutoMapperProfie : Profile
    {
        public AutoMapperProfie()
        {
            CreateMap<UserInfo, UserInfoDto>()
                .ForMember(x => x.Roles, opt => opt.MapFrom(y => y.UserRoleLink.Select(u => u.Role.Name).Join(",")))
                .ForMember(x => x.RoleIds, opt => opt.MapFrom(y => y.UserRoleLink.Select(u => u.RoleId)));
            CreateMap<UserInfoDto, UserInfo>();

            CreateMap<RoleDto, SysCoreRole>();
            CreateMap<SysCoreRole, RoleDto>();

            CreateMap<SystemMenuList, SystemMenuListDto>();
            CreateMap<SysCoreMenu, SystemMenuDto>();

            CreateMap<SysCoreElement, SystemElementDto>();

            CreateMap<UserCheckInfo, UserCheckDto>();

            CreateMap<SupplierInfoDto, SupplierInfo>()
                .ForMember(x => x.CreateUser, opt => opt.Ignore());
            CreateMap<SupplierInfo, SupplierInfoDto>()
                .ForMember(x => x.CreateUser, opt => opt.MapFrom(y => y.UserInfo.UserName));

            CreateMap<SupplierGoodsInfoDto, SupplierGoodsInfo>()
                .ForMember(x => x.CreateUser, opt => opt.Ignore())
                .ForMember(x => x.State, opt => opt.Ignore());

            CreateMap<SupplierGoodsInfo, SupplierGoodsInfoDto>()
                .ForMember(x => x.CreateUser, opt => opt.MapFrom(y => y.UserInfo.UserName));

            CreateMap<UserCheckInfo, UserCheckDto>()
                .ForMember(x => x.UserName, opt => opt.MapFrom(y => y.UserInfo.UserName))
                .ForMember(x => x.Roles, opt => opt.MapFrom(y => y.UserInfo.UserRoleLink.Select(x => x.Role.Name).Join(",")));

            CreateMap<UserInfo, UserRoleDto>()
                .ForMember(x => x.Roles, opt => opt.MapFrom(y => y.UserRoleLink.Select(x => x.Role.Name).Join(",")));

            CreateMap<SysCoreRoleExtra, SysCoreRoleExtraDto>();

            CreateMap<ProjectInfoDto, ProjectInfo>();
            CreateMap<SysCoreRoleExtra, SysCoreRoleExtraDto>();

            CreateMap<ProjectInfo, ProjectInfoDto>();

            CreateMap<ProjectUserInfo, ProjectUserInfoDto>()
                .ForMember(x => x.UserName, opt => opt.MapFrom(y => y.User.UserName))
                .ForMember(x => x.NickName, opt => opt.MapFrom(y => y.User.NickName))
                .ForMember(x => x.Roles, opt => opt.MapFrom(y => y.User.UserRoleLink.Select(x => x.Role.Name).Join(",")));

            CreateMap<ProjectUserIdDto, ProjectUserInfo>()
                .ForMember(x => x.Id, opt => opt.Ignore())
                .ForMember(x => x.CreateTime, opt => opt.Ignore())
                .ForMember(x => x.CreateUser, opt => opt.Ignore());

            CreateMap<ProjectItemInfoDto, ItemInfo>();

            CreateMap<ItemInfo, ProjectItemInfoDto>()
                .ForMember(x => x.Images, opt => opt.MapFrom(y => y.ItemImageInfos));
            CreateMap<ItemImageInfo, ProjectItemImageInfoDto>();

            CreateMap<ProjectItemImageInfoDto, ItemImageInfo>()
                .ForMember(x => x.Id, opt => opt.Ignore())
                .ForMember(x => x.CreateTime, opt => opt.Ignore())
                .ForMember(x => x.ItemId, opt => opt.Ignore());

            CreateMap<CostInfo, CostApplyDto>()
                .ForMember(x => x.ProjectItemName, opt => opt.MapFrom(y => y.ProjectItemInfo.ItemName))
                .ForMember(x => x.ProjectName, opt => opt.MapFrom(y => y.ProjectInfo.ProjectName))
                .ForMember(x => x.CostType, opt => opt.MapFrom(y => y.CostType.Name))
                .ForMember(x => x.UserName, opt => opt.MapFrom(y => y.UserInfo.UserName))
                .ForMember(x => x.CreateUserId, opt => opt.MapFrom(y => y.CreateUser))
                .ForMember(x => x.Images, opt => opt.MapFrom(y => y.Images.Select(x => x.ImageUrl)))

                .ForMember(x => x.FinancerApproveUser, opt => opt.MapFrom(y => y.FinancerApproveUserInfo.UserName))
                .ForMember(x => x.ManagerApproveUser, opt => opt.MapFrom(y => y.ManagerApproveUserInfo.UserName))
                ;

            CreateMap<CostApplyDto, CostInfo>()
                .ForMember(x => x.CreateUser, opt => opt.MapFrom(y => y.CreateUserId))
                .ForMember(x => x.Images, opt => opt.Ignore());


            CreateMap<CostTypeDto, CostType>()
                .ForMember(x => x.UserInfo, opt => opt.Ignore());

            CreateMap<CostType, CostTypeDto>()
                .ForMember(x => x.CreateUser, opt => opt.MapFrom(y => y.UserInfo.UserName))
                ;

            CreateMap<SysCoreMenu, RoleAuthMenu>()
                .ForMember(x => x.Id, opt => opt.MapFrom(o => o.Id))
                .ForMember(x => x.ParentId, opt => opt.MapFrom(o => o.ParentId))
                .ForMember(x => x.Name, opt => opt.MapFrom(o => o.Name))
                .ForMember(x => x.Elements, opt => opt.MapFrom(o => o.Elements))
                .ForMember(x => x.HasAuth, opt => opt.Ignore())
                ;
            CreateMap<SysCoreElement, RoleAuthElement>()
                .ForMember(x => x.Id, opt => opt.MapFrom(o => o.Id))
                .ForMember(x => x.Name, opt => opt.MapFrom(o => o.Name))
                .ForMember(x => x.HasAuth, opt => opt.Ignore())
                ;

            CreateMap<SysCoreRoleExtraDto, SysCoreRoleExtra>()
                .ForMember(x => x.RoleId, opt => opt.Ignore())
                ;

            CreateMap<WorkerTypeInfoDto, WorkerTypeInfo>()
                .ForMember(x => x.Status, opt => opt.Ignore());
            CreateMap<WorkerTypeInfo, WorkerTypeInfoDto>();

            CreateMap<UserCheckDetail, UserCheckDetailDto>()
                .ForMember(x => x.WorkerType, opt => opt.MapFrom(o => o.WorkerTypeInfo.Name));
            CreateMap<UserCheckDetailDto, UserCheckDetail>();

            CreateMap<UserCheckSqlDto, UserCheckDto>()
                .ForMember(x => x.Id, opt => opt.MapFrom(o => o.UserCheckInfo.Id))
                .ForMember(x => x.BelongsDate, opt => opt.MapFrom(o => o.UserCheckInfo.BelongsDate))
                .ForMember(x => x.ClockInDate, opt => opt.MapFrom(o => o.UserCheckInfo.ClockInDate))
                .ForMember(x => x.ClockInPicUrl, opt => opt.MapFrom(o => o.UserCheckInfo.ClockInPicUrl))
                .ForMember(x => x.ClockOutDate, opt => opt.MapFrom(o => o.UserCheckInfo.ClockOutDate))
                .ForMember(x => x.ClockOutPicUrl, opt => opt.MapFrom(o => o.UserCheckInfo.ClockOutPicUrl))
                .ForMember(x => x.UserId, opt => opt.MapFrom(o => o.UserCheckInfo.UserId))
                .ForMember(x => x.UserName, opt => opt.MapFrom(o => o.UserName))
                .ForMember(x => x.Roles, opt => opt.MapFrom(o => o.Roles))
                .ForMember(x => x.Detail, opt => opt.MapFrom(o => o.Details))
                ;

            CreateMap<AddUserCheckDetailDto, UserCheckDetail>()
                .ForMember(x => x.UserCheckId, opt => opt.MapFrom(o => o.UserCheckInfoId))
                .ForMember(x => x.Status, opt => opt.Ignore())
                ;
            CreateMap<ProjectItemData, ItemInfo>()
                .ForMember(x => x.Id, opt => opt.Ignore())
                .ForMember(x => x.ProjectId, opt => opt.Ignore())
                .ForMember(x => x.ItemNo, opt => opt.MapFrom(o => o.ItemNo))
                .ForMember(x => x.ItemName, opt => opt.MapFrom(o => o.ItemName))
                .ForMember(x => x.ItemDesc, opt => opt.MapFrom(o => o.ItemDesc))
                .ForMember(x => x.BudgetAmount, opt => opt.MapFrom(o => o.BudgetAmountValue))
                .ForMember(x => x.BudgetDosage, opt => opt.MapFrom(o => o.BudgetDosageValue))
                .ForMember(x => x.FactAmount, opt => opt.MapFrom(o => o.FactAmountValue))
                .ForMember(x => x.FactDosage, opt => opt.MapFrom(o => o.FactDosageValue))
                .ForMember(x => x.Unit, opt => opt.MapFrom(o => o.ItemUnit))
                .ForMember(x => x.CreateTime, opt => opt.Ignore())
                .ForMember(x => x.CreateUser, opt => opt.Ignore())
                .ForMember(x => x.State, opt => opt.Ignore())
                .ForMember(x => x.ItemImageInfos, opt => opt.Ignore())
                .ForMember(x => x.CostInfos, opt => opt.Ignore())
                ;

            CreateMap<AddBuildingSiteBillDto, BuildingSiteBill>()
                .ForMember(x => x.Id, opt => opt.Ignore())
                .ForMember(x => x.CreateUser, opt => opt.Ignore())
                .ForMember(x => x.CreateTime, opt => opt.MapFrom(y => DateTime.Now))
                .ForMember(x => x.CreateUserInfo, opt => opt.Ignore())
                .ForMember(x => x.Images, opt => opt.MapFrom(y => y.Images.Select(s => new BuildingSiteBillImages { Image = s })))
                ;
            CreateMap<UpdBuildingSiteBillDto, BuildingSiteBill>()
                .ForMember(x => x.CreateUser, opt => opt.Ignore())
                .ForMember(x => x.CreateTime, opt => opt.Ignore())
                .ForMember(x => x.CreateUserInfo, opt => opt.Ignore())
                .ForMember(x => x.Images, opt => opt.MapFrom(y => y.Images.Select(s => new BuildingSiteBillImages { Image = s })))
                ;

            CreateMap<BuildingSiteBill, BuildingSiteBillDto>()
                .ForMember(x => x.UnitPrice, opt => opt.MapFrom(y => Math.Round(y.UnitPrice, 2)))
                .ForMember(x => x.TotalPrice, opt => opt.MapFrom(y => Math.Round(y.UnitPrice * y.Quantity, 2)))

                .ForMember(x => x.CreateUser, opt => opt.MapFrom(y => y.CreateUserInfo.UserName))
                .ForMember(x => x.CreateTime, opt => opt.MapFrom(y => y.CreateTime.ToString("yyyy-MM-dd HH:mm:ss")))

                .ForMember(x => x.ReturnUser, opt => opt.MapFrom(y => y.ReturnUserInfo.UserName))
                .ForMember(x => x.ReturnTime, opt => opt.MapFrom(y => y.ReturnTime.HasValue
                    ? y.ReturnTime.Value.ToString("yyyy-MM-dd HH:mm:ss")
                    : null))

                .ForMember(x => x.ConfirmUser, opt => opt.MapFrom(y => y.ConfirmUserInfo.UserName))
                .ForMember(x => x.ConfirmTime, opt => opt.MapFrom(y => y.ConfirmTime.HasValue
                      ? y.ConfirmTime.Value.ToString("yyyy-MM-dd HH:mm:ss")
                      : null))

                .ForMember(x => x.ProjectName, opt => opt.MapFrom(y => y.ProjectInfo.ProjectName))
                .ForMember(x => x.Images, opt => opt.MapFrom(y => y.Images.Select(s => s.Image)))
                ;

            CreateMap<PreFinalAccountFile, PreFinalAccountFileDto>()
                .ForMember(x => x.CreateTime, opt => opt.MapFrom(y => y.CreateTime.ToString("yyyy-MM-dd HH:mm:ss")))
                .ForMember(x => x.CreateUser, opt => opt.MapFrom(y => y.CreateUserInfo.UserName))
                .ForMember(x => x.AuditTime, opt => opt.MapFrom(y => y.AuditTime.HasValue ?
                      y.AuditTime.Value.ToString("yyyy-MM-dd HH:mm:ss") : string.Empty))
                .ForMember(x => x.AuditUser, opt => opt.MapFrom(y => y.AuditUserInfo.UserName))
                .ForMember(x => x.ProjectName, opt => opt.MapFrom(y => y.ProjectInfo.ProjectName))
                ;

            CreateMap<AddPreFinalAccountFile, PreFinalAccountFile>()
                .ForMember(x => x.Id, opt => opt.MapFrom(y => 0))
                .ForMember(x => x.CreateTime, opt => opt.MapFrom(y => DateTime.Now))
                .ForMember(x => x.CreateUser, opt => opt.Ignore())
                .ForMember(x => x.AuditUser, opt => opt.Ignore())
                .ForMember(x => x.AuditTime, opt => opt.Ignore())
                .ForMember(x => x.AuditComment, opt => opt.Ignore())
                .ForMember(x => x.Status, opt => opt.MapFrom(y => (byte)PreFinalAccountFileAuditEnums.Waiting))
                .ForMember(x => x.CreateUserInfo, opt => opt.Ignore())
                .ForMember(x => x.AuditUserInfo, opt => opt.Ignore())
                ;

            CreateMap<ProjectUserInfoSql, ProjectUserInfo>()
                .ForMember(x => x.User, opt => opt.Ignore())
                ;

            CreateMap<ProjectItemPriceRecord, ProjectItemPriceRecordDto>()
                .ForMember(x => x.CreateUser, opt => opt.MapFrom(y => y.UserInfo.UserName))
                ;


            CreateMap<AddPurchaseOrderDto, PurchaseOrder>()
                .ForMember(x => x.Id, opt => opt.Ignore())
                .ForMember(x => x.CreateTime, opt => opt.Ignore())
                .ForMember(x => x.CreateUser, opt => opt.Ignore())
                .ForMember(x => x.CreateUserInfo, opt => opt.Ignore())
                .ForMember(x => x.BuyerUserInfo, opt => opt.Ignore())
                ;

            CreateMap<PurchaseOrder, PurchaseOrderDto>()
                .ForMember(x => x.BuyerUser, opt => opt.MapFrom(y => y.BuyerUserInfo.UserName))
                .ForMember(x => x.CreateTime,
                    opt => opt.MapFrom(y => y.CreateTime.ToString("yyyy-MM-dd HH:mm:ss")))
                .ForMember(x => x.CreateUser, opt => opt.MapFrom(y => y.CreateUserInfo.UserName))
                ;

            CreateMap<SysUpgradeDto, SysUpgrade>()
                .ForMember(x=>x.Id,opt=>opt.Ignore())
                .ForMember(x => x.CreateTime, opt => opt.Ignore());

            CreateMap<SysUpgrade, SysUpgradeDto>();
        }
    }
}
